package com.example.tt_bells_v02;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class AdapterBell extends BaseAdapter implements OnClickListener {

	List<Bell> 	bellHolder = null;
	List<Bell> 	todayHolder = new ArrayList<Bell>();
	Time 		today = null;
	Activity	alink = null;
	
	public AdapterBell(List<Bell> datalink, Time selected_day) {
		super();
		
		today = selected_day;
		bellHolder = datalink;
		
		refreshToday();
	}
	
	public void refreshToday() {
		todayHolder.clear();
		boolean ok = false;
		
		for (Bell element : bellHolder) { //analyze all Bells, if they are ok for today
			ok = false;
			switch (element.bell_type) {
				case BellInfo.BELL_TYPE_MULTIPLE: {
					
				break; }
				case BellInfo.BELL_TYPE_RANDOM: {
					
				break; }
				case BellInfo.BELL_TYPE_PERIODIC: {
					ok = DayCompare.isBetweenDays(element.alarm_time, element.last_alarm_time, today);
					Log.d(Tag.TAG,"periodic "+element.name_short+" ok="+ok);
				break; }
				case BellInfo.BELL_TYPE_SINGLE: {
					ok = DayCompare.isSameDay(today,element.alarm_time);
					Log.d(Tag.TAG,"single "+element.name_short+" ok="+ok);
				break; }
			}
			if (ok) {
				todayHolder.add(element);
			}
		}
	}
/*	
	//static can be called from other classes
	//TODO: move out time comparations to new class or use other time api 
	static public boolean isSameDay(Time day1, Time day2) {
		//Workaround exception when day2 HMS!=0, but we need allDay=true
		//Time d2 = new Time(day2);
		//d2.hour = 0; d2.minute = 0; d2.second = 0;
		
		//SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
		//return fmt.format(day1).equals(fmt.format(day2));
		//Log.d(Tag.TAG,"day1: "+day1.format3339(true));
		//Log.d(Tag.TAG,"day2: "+d2.format3339(true));
		//return day1.format3339(true).equals(d2.format3339(true));
		return day1.format("%Y%m%d").equals(day2.format("%Y%m%d"));
	}
	
	public boolean isBetweenDays(Time first_day, Time last_day, Time current_day) {
		boolean ok1;
		boolean ok2;
		//workaround
		Time d1 = new Time(first_day);
		Time d2 = new Time(last_day);
		d1.hour = 0; d1.minute = 0; d1.second = 0;
		d2.hour = 0; d2.minute = 0; d2.second = 0;
		
		//Log.d(Tag.TAG,"first_day: "+d1.format3339(true));
		//Log.d(Tag.TAG,"last_day: "+d2.format3339(true));
		//Log.d(Tag.TAG,"current_day: "+current_day.format3339(true));
		ok1 = ((d1.format("%Y%m%d").equals(current_day.format("%Y%m%d"))) || (current_day.after(d1)));
		ok2 = ((d2.format("%Y%m%d").equals(current_day.format("%Y%m%d"))) || (current_day.before(d2)));
		
		return (ok1 && ok2);
	}
*/	
	@Override
	public int getCount() {
		//return bellHolder.size();
		return todayHolder.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Context context = parent.getContext();
		//Bell selected = bellHolder.get(position);
		Bell selected = todayHolder.get(position);
		
		View retval = LayoutInflater.from(context).inflate(R.layout.list_item_bell, null);
		TextView sli_maintext = (TextView)retval.findViewById(R.id.sli_maintext);
		TextView sli_timetext = (TextView)retval.findViewById(R.id.sli_timetext);
		TextView sli_addtext = (TextView)retval.findViewById(R.id.sli_addtext);
		ImageView sli_delete = (ImageView)retval.findViewById(R.id.sli_delete);
		CheckBox sli_enable = (CheckBox)retval.findViewById(R.id.sli_enable);
		RelativeLayout sli_rl = (RelativeLayout)retval.findViewById(R.id.sli_layout);
		
		sli_maintext.setText(selected.name_short);
		sli_enable.setChecked(selected.isEnabled());
		//TODO: apply timetext
		sli_timetext.setText(selected.name_long);
		
		sli_rl.setTag(position);
		sli_rl.setOnClickListener(this);
		sli_rl.setClickable(true);
		
		sli_delete.setTag(position);
		sli_delete.setOnClickListener(this);
		sli_delete.setClickable(true);
		
		sli_enable.setOnClickListener(this);
		sli_enable.setTag(position);
		
		if (selected.bell_type == BellInfo.BELL_TYPE_PERIODIC) {
			if (!DayCompare.isSameDay(selected.alarm_time,selected.last_alarm_time)) {
				sli_addtext.setText("Start: "+selected.alarm_time.format("%Y.%m.%d/%H:%M") +", End:"+selected.last_alarm_time.format("%Y.%m.%d/%H:%M"));
			}
		}
		
		return retval;
	}

	@Override
	public void onClick(View v) {
		if (v == null) {
			Log.d(Tag.TAG,"View = null !!!");
			return;
		}
		int pos = Integer.parseInt(v.getTag().toString());
		Bell clicked_bell = todayHolder.get(pos);
		int real_pos = bellHolder.indexOf(clicked_bell);
		Log.d(Tag.TAG, "pos="+pos+", real_pos="+real_pos);
		
		switch (v.getId()) {
			case R.id.sli_layout: {	
				Intent aaa = new Intent(v.getContext(),ActivityEditBellInfo.class);
				
				aaa.putExtra("index",real_pos);
				aaa.putExtra("type", BellInfo.EDIT_BELL);
				v.getContext().startActivity(aaa);	
			break; }
			case R.id.sli_delete: {
				//TODO: add setupable checkbox - is confirmation-on-delete needed or not
				Intent aaa = new Intent(v.getContext(),ActivityDeleteBell.class);
				aaa.putExtra("edit_type", BellInfo.EDIT_BELL);
				if (clicked_bell.bell_type == BellInfo.BELL_TYPE_PERIODIC)
					aaa.putExtra("several_days", !DayCompare.isSameDay(clicked_bell.alarm_time, clicked_bell.last_alarm_time));
				
				alink.startActivityForResult(aaa, real_pos);
			break; }
			case R.id.sli_enable: {
				if (clicked_bell.isEnabled()) { //Disable
					clicked_bell.deactivate();
				} else { //Enable
					clicked_bell.activate();
				}
			break; }
		}
	
	}
	
}
